<template>
  <div wg-cart>
    <div
      class="wg-cart-item"
      v-for="item in list"
      :key="item.id"
    >
      <input  v-if="item.checked === true" type="checkbox" @click="changeState(item.id)" checked="checked">
      <input  v-else type="checkbox" @click="changeState(item.id)">
      <div class="wg-cart-item-left">
        <img :src="'https://cdn.bestseller.com.cn' + item.src" alt="">
      </div>
      <div class="wg-cart-item-right">
        <div class="wg-cart-item-right-name">{{item.name}}</div>
        <div class="wg-cart-item-right-price">￥{{item.price}}</div>
        <div class="wg-cart-item-right-count">
          <span class="wg-cart-item-right-count-reduce" @click="reduceAmount(item.id)">-</span>
          <span class="amount">{{item.count}}</span>
          <span class="wg-cart-item-right-count-add" @click="addAmount(item.id)">+</span>
        </div>
      </div>
      <mt-button
        type="primary"
        size="small"
        class="deleteItem"
        @click="deleteItem(item.id)"
      >
        删除
      </mt-button>
    </div>
    <div v-if='this.list.length > 0' class="wg-cart-totalprice">总价￥{{totalPrice.toFixed(2)}}元</div>
    <div v-if='this.list.length === 0' @click='goshopping' class='goshopping'>您的购物车为空，再去逛逛吧</div>
  </div>
</template>

<script>
import {
  mapState,
  mapMutations,
  mapGetters
} from 'vuex'
export default {
  name: 'Cart',
  methods: {
    ...mapMutations(['reduceAmount', 'addAmount', 'changeState', 'deleteItem']),
    goshopping () {
      this.$router.push('/mall')
    }
  },
  computed: {
    ...mapState(['list']),
    ...mapGetters(['totalPrice'])
  }
}
</script>

<style lang="scss" scoped>
.goshopping{
  width: 100%;
  height: 500px;
  text-align: center;
  line-height: 500px;
  background-color:#00FFFF;
  color: white;
  font-size: 35px;
}
  .wg-cart-totalprice {
    text-align: center;
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 20px;
  }
  .wg-cart {
    width: 100%;

    &-item {
      width: 100%;
      height: 162px;
      margin-bottom: 20px;
      input {
        float: left;
        margin-top: 75px;
      }

      &-left {
        float: left;
        width: 30%;
        height:100%;
        img {
          display: block;
          width: 90%;
          height: 100%;
        }
      }

      &-right {
        float: left;
        width: 40%;
        height: 100%;
        margin-left: 20px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        &-count {
          width: 88px;
          display: flex;
          justify-content: space-around;
          border: 1px solid #ccc;

          span {
            width: 28px;
            height: 28px;
            text-align: center;
            line-height: 28px;
            float: left;
          }

          &-reduce {
            cursor: pointer;
            border-right: 1px solid #ccc;
          }
          &-add {
            cursor: pointer;
            border-left: 1px solid #ccc;
          }
        }
      }
    }
  }
</style>
<style lang="scss">
  .deleteItem {
    float: right;
    margin-right: 20px;
    margin-top: 55px;
  }
</style>
